package com.ee.chat.cps.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ee.chat.common.bean.Result;
import com.ee.chat.common.dao.UserInviteLogMapper;
import com.ee.chat.common.entity.Channel;
import com.ee.chat.cps.admin.service.IChannelOperationService;
import com.ee.chat.cps.admin.service.IPromoteUserService;
import com.ndhutu.ee.util.DateUtil;
import com.ndhutu.ee.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author AppiJ
 * @Date 2020/4/1 16:57
 */
@Service
public class PromoteUserServiceImpl implements IPromoteUserService {

    @Resource
    UserInviteLogMapper userInviteLogMapper;
    @Resource
    IChannelOperationService operationService;

    /**
     * 获取推广用户列表
     * @Author AppiJ
     * @Date 2020/4/1 16:23
     * @param user  用户信息
     * @param time  日期时间
     * @return
     */
    @Override
    public Result list(HttpServletRequest request, String user, Integer type, String time, Integer page, Integer limit) throws Exception {
        Map<String, Object> data = new HashMap<>();

        HttpSession session = request.getSession(true);
        Channel channel = (Channel) session.getAttribute("channelUser");

        Date beginTime = null, endTime = null;
        if(!StringUtil.isEmpty(time)){
            String[] arr = time.split(" - ");
            beginTime = DateUtil.parse(arr[0]);
            endTime = DateUtil.parse(arr[1]);
        }

        Page<Map<String, Object>> p = new Page<>(page, limit);
        IPage<Map<String, Object>> iPage = userInviteLogMapper.queryPromoteUserList(p, channel.getUserId(), user, type, beginTime, endTime);
        List<Map<String, Object>> promoteUsers = iPage.getRecords();

        data.put("promoteUsers", promoteUsers);
        data.put("total", iPage.getTotal());

        operationService.addOperationLog(request, "查看推广用户列表数据");

        return Result.returnSuccess(data);
    }
}
